//
//  MineViewController.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/4.
//

import UIKit

class MineViewController: BaseViewController {
    var dataSourceArr: [MineModel] = [MineModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavi()
        setupUI()
        loadTestData()
    }
    private func setupNavi() {
        navigationItem.title = "个人中心"
    }
    private func setupUI() {
        view.addSubviews(headerView, tableView)
        headerView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(166)
        }
        tableView.snp.makeConstraints { make in
            make.top.equalTo(headerView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    private func loadTestData() {
        dataSourceArr.append(MineModel(iconStr: "mine_account_safe_icon", titleStr: "账号与安全", type: 1))
        dataSourceArr.append(MineModel(iconStr: "mine_device_message_icon", titleStr: "设备信息", type: 2))
        dataSourceArr.append(MineModel(iconStr: "mine_buy_msg_icon", titleStr: "购买记录", type: 3))
        dataSourceArr.append(MineModel(iconStr: "mine_tui_kuan_icon", titleStr: "退款申请", type: 4))
        dataSourceArr.append(MineModel(iconStr: "mine_chang_jian_question_icon", titleStr: "常见问题", type: 5))
        dataSourceArr.append(MineModel(iconStr: "mine_question_report_icon", titleStr: "问题反馈", type: 6))
        dataSourceArr.append(MineModel(iconStr: "mine_about_icon", titleStr: "关于我们", type: 7))
        tableView.reloadData()
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var headerView: MineHeaderView = {
        let headerView = MineHeaderView()
        headerView.signInButtonActionBlock = {
            Toast.with(message: "签到").showOnWindow()
        }
        headerView.shareButtonActionBlock = {
            Toast.with(message: "分享").showOnWindow()
        }
        return headerView
    }()
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerCellClass(MineTableViewCell.self)
        tableView.backgroundColor = .clear
        return tableView
    }()
}

extension MineViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSourceArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(MineTableViewCell.self, for: indexPath)
        cell.model = dataSourceArr[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        47
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = dataSourceArr[indexPath.row]
        switch model.type {
        case 1:
            let vc = MineAccountViewController()
            navigationController?.pushViewController(vc, animated: true)
        case 2:
            let vc = MineDeviceMessageViewController()
            navigationController?.pushViewController(vc, animated: true)
        case 3:
            let vc = MineBuyMessageViewController()
            navigationController?.pushViewController(vc, animated: true)
        case 4:
            let vc = MineRefundViewController()
            navigationController?.pushViewController(vc, animated: true)
        case 5:
            let vc = MineQAViewController()
            navigationController?.pushViewController(vc, animated: true)
        case 6:
            let vc = FeedbackOnTheIssueQAViewController()
            navigationController?.pushViewController(vc, animated: true)
        case 7:
            let vc = MineAboutViewController()
            navigationController?.pushViewController(vc, animated: true)
            
        default:
            break
        }
    }
}
